package com.pmcc.cj.web;

import com.pmcc.cj.entity.*;
import com.pmcc.cj.repository.CjOrderRepository;
import com.pmcc.cj.service.*;
import com.pmcc.utils.AntdPage;
import com.pmcc.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * Created by yaonan on 2018/3/26.
 */
@RestController()
@RequestMapping("api/v1/web")
public class WebController {
    @Autowired
    CjCommodityService cjCommodityService;
    @Autowired
    CjTrainService cjTrainService;
    @Autowired
    CjTrainDetailService cjTrainDetailService;
    @Autowired
    CjCommodityYunService cjCommodityYunService;
    @Autowired
    CjAreaAgencyService cjAreaAgencyService;
    @Autowired
    FileService fileService;
    @Autowired
    FjCityService fjCityService;
    @Autowired
    CjCarouselService cjCarouselService;
    @Autowired
    CjCountyProxyService cjCountyProxyService;
    @Autowired
    CjRetailerService cjRetailerService;
    @Autowired
    CjRobotAttestService cjRobotAttestService;
    @Autowired
    CjActivationSaleService cjActivationSaleService;
    @Autowired
    CjFarmService cjFarmService;
    @Autowired
    CjRuleService cjRuleService;
    @Autowired
    CjOrderIndexService cjOrderIndexService;
    @Autowired
    CjOrderService cjOrderService;
    /**
     * 新增普通商品
     * @param model
     * @return
     */
    @PostMapping("addCommodity")
    public ResultBean addCommodity(CjCommodity model){
        return cjCommodityService.addCommodity(model);
    }

    /**
     * 删除普通商品
     * @param id
     * @return
     */
    @GetMapping("deleteCommodity")
    public ResultBean deleteCommodity(String id){
        return cjCommodityService.deleteCommodity(id);
    }

    /**
     * 修改普通商品
     * @param map
     * @return
     */
    @PostMapping("modifyCommodity")
    public ResultBean modifyCommodity(Map map){
        return cjCommodityService.modifyCommodity(map);
    }

    /**
     * 查询所有普通商品
     * @return
     */
    @GetMapping("queryCommodity")
    public ResultBean queryCommodity(){
        return cjCommodityService.queryCommodity();
    }

    /**
     * 分页查询所有培训
     * @param antdPage
     * @param model
     * @return
     */
    @GetMapping(value = "queryTrainByPage")
    public ResultBean queryTrainByPage(AntdPage antdPage, CjTrain model){
        return cjTrainService.queryTrainByPage(antdPage, model);
    }
    /**
     * 新增培训内容
     * @param model
     * @return
     */
    @PostMapping("addTrain")
    public ResultBean addTrain(@RequestBody CjTrain model){
        return cjTrainService.addTrain(model);
    }

    /**
     * 删除培训内容
     * @param id
     * @return
     */
    @GetMapping("deleteTrain")
    public ResultBean deleteTrain(String id){
        return cjTrainService.deleteTrain(id);
    }

    /**
     * 修改培训内容
     * @param map
     * @return
     */
    @PostMapping("modifyTrain")
    public ResultBean modifyTrain(@RequestBody Map map){
        return cjTrainService.modifyTrain(map);
    }

    /**
     * 倒叙查询所有培训内容
     * @return
     */
    @GetMapping("queryTrain")
    public ResultBean queryTrain(){
        return cjTrainService.queryTrain();
    }

    /**
     * 新增培训报名人员
     * @param model
     * @return
     */
    @PostMapping("addTrainDetail")
    public ResultBean addTrainDetail(CjTrainDetail model){
        return cjTrainDetailService.addTrainDetail(model);
    }

    /**
     * 取消培训报名
     * @param id
     * @return
     */
    @GetMapping("deleteTrainDetail")
    public ResultBean deleteTrainDetail(String id){
        return cjTrainDetailService.deleteTrainDetail(id);
    }

    /**
     * 查询所有该培训的报名人员
     * @param trainId
     * @return
     */
    @GetMapping("queryTrainDetail")
    public ResultBean queryTrainDetail(String trainId){
        return cjTrainDetailService.queryTrainDetail(trainId);
    }

    /**
     * 根据当前登录人查询已报名的培训项目
     * @return
     */
    @GetMapping("queryTrainDetailEmployee")
    public ResultBean queryTrainDetailEmployee(){
        return cjTrainDetailService.queryTrainDetailEmployee();
    }



    /**
     * 云飞防获取商品列表
     * @param model 农作物类型（如玉米，大豆）   药物类型（如飞防杀菌，飞防除草，飞防套餐）
     * @param  antdPage
     * @return  商品缩略图，商品id，商品规格和单价【20ml,3,使用说明】[30ml,4，使用说明][40ml,5,使用说明],商品作业简介 ,界面是否组合优惠
     *
     */
    @GetMapping(value= "queryCommodityYunByPage")
    public ResultBean queryCommodityYunByPage(CjCommodityYun model, AntdPage antdPage){
        return cjCommodityYunService.queryCommodityYunByPage(model, antdPage);
    }

    /**
     * 只查询云飞防的指定code的子类节点
     * @param code  SPFL    GNFL
     * @return
     */
    @GetMapping("commodityYunDict")
    public ResultBean commodityYunDict(String code){
        return cjCommodityYunService.commodityYunDict(code);
    }
    /**
     * 新增云飞防商品
     * @param model
     * @return
     */
    @PostMapping("addCommodityYun")
    public ResultBean addCommodityYun(@RequestBody CjCommodityYun model){
        return cjCommodityYunService.addCommodityYun(model);
    }

    /**
     * 新增云飞防商品剂量表
     * @param model
     * @return
     */
    @PostMapping("addCommodityYunDetail")
    public ResultBean addCommodityYunDetail(@RequestBody CjCommodityYunDetail model){
        return cjCommodityYunService.addCommodityYunDetail(model);
    }

    /**
     * 删除云飞防商品
     * @param id
     * @return
     */
    @GetMapping("deleteCommodityYun")
    public ResultBean deleteCommodityYun(String id){
        return cjCommodityYunService.deleteCommodityYun(id);
    }

    /**
     * 删除云飞防商品剂量
     * @param id
     * @return
     */
    @GetMapping("deleteCommodityYunDetail")
    public ResultBean deleteCommodityYunDetail(String id){
        return cjCommodityYunService.deleteCommodityYunDetail(id);
    }

    /**
     * 修改云飞防商品
     * @param map
     * @return
     */
    @PostMapping("modifyCommodityYun")
    public ResultBean modifyCommodityYun(@RequestBody Map map){
        return cjCommodityYunService.modifyCommodityYun(map);
    }

    /**
     * 修改云飞防商品剂量
     * @param map
     * @return
     */
    @PostMapping("modifyCommodityYunDetail")
    public ResultBean modifyCommodityYunDetail(@RequestBody Map map){
        return cjCommodityYunService.modifyCommodityYunDetail(map);
    }

    /**
     * 云飞防剂量表关联其他剂量数据查询
     * @param id    关联表主键id
     * @param yunId   关联表父级id
     * @return
     */
    @GetMapping("detailRelated")
    public ResultBean detailRelated(String id,String yunId){
        return cjCommodityYunService.detailRelated(id, yunId);
    }

    /**
     * 云飞防剂量表关联
     * @param id    主表
     * @param relateId  关联表
     * @return
     */
    @GetMapping("detailRelatedConfirm")
    public ResultBean detailRelatedConfirm(String id,String relateId){
        return cjCommodityYunService.detailRelatedConfirm(id, relateId);
    }

    /**
     * 分页查询区域机构
     * @param model
     * @param antdPage
     * @return
     */
    @GetMapping(value= "queryAreaAgencyByPage")
    public ResultBean queryAreaAgencyByPage(CjAreaAgency model, AntdPage antdPage){
        return cjAreaAgencyService.queryAreaAgencyByPage(model, antdPage);
    }

    /**
     * 用于区域首页面
     * @return
     */
    @GetMapping(value= "queryAreaAgencyTreeGrid")
    public ResultBean queryAreaAgencyTreeGrid() {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        resultBean.setRows(cjAreaAgencyService.queryAreaAgencyTreeGrid());
        return resultBean;
    }
    /**
     * 新增区域机构
     * @param model
     * parentId 父类id
     * address  区域
     * code 区域编码
     * @return
     */
    @PostMapping("addAreaAgency")
    public ResultBean addAreaAgency(@RequestBody CjAreaAgency model){
        return cjAreaAgencyService.addAreaAgency(model);
    }

    /**
     *县级代理认证填激活码        此处逻辑应该在县级代理认证审核之后
     * @param id    区域机构
     * @param employeeId    申请人
     * @return
     */
//    @GetMapping("setAreaAgency")
//    public ResultBean setAreaAgency(String id, String employeeId){
//        return cjAreaAgencyService.setAreaAgency(id,employeeId);
//    }

    /**
     * 假删区域机构
     * @param id
     * @return
     */
    @GetMapping("deleteAreaAgency")
    public ResultBean deleteAreaAgency(String id){
        return cjAreaAgencyService.deleteAreaAgency(id);
    }

    /**
     * 修改区域机构
     * @param map
     * @return
     */
    @PostMapping("modifyAreaAgency")
    public ResultBean modifyAreaAgency(@RequestBody Map map){
        return cjAreaAgencyService.modifyAreaAgency(map);
    }

    /**
     * 查询指定区域机构
     * @param id
     * @return
     */
    @GetMapping("queryAreaAgencyById")
    public ResultBean queryAreaAgencyById(String id){
        return cjAreaAgencyService.queryAreaAgencyById(id);
    }

    /**
     * 查询指定父类的字节点
     * @param parentId
     * @return
     */
    @GetMapping("findAreaAgency")
    public ResultBean findAreaAgency(String parentId){
        return cjAreaAgencyService.findAreaAgency(parentId);
    }

    /**
     * 上传文件
     * @param files
     * @return
     */
    @PostMapping("uploadFile")
    public ResultBean uploadFile(@RequestParam(value = "files",required = false)MultipartFile[] files){
        return fileService.uploadFile(files);
    }

    /**
     * 新增广告详情
     * @param model
     * @return
     */
    @PostMapping(value= "addAnnouncement")
    public ResultBean addAnnouncement(@RequestBody CjCarousel model){
        return cjCarouselService.addAnnouncement(model);
    }

    /**
     * 分页查询广告详情
     * @param model
     * @param antdPage
     * @return
     */
    @GetMapping(value= "queryAnnouncementByPage")
    public ResultBean queryAnnouncementByPage(CjCarousel model, AntdPage antdPage){
        return cjCarouselService.queryAnnouncementByPage(model, antdPage);
    }

    /**
     * 修改广告详情
     * @param map
     * @return
     */
    @PostMapping("modifyAnnouncement")
    public ResultBean modifyAnnouncement(@RequestBody Map map){
        return cjCarouselService.modifyAdvertisement(map);
    }

    /**
     * 删除广告详情
     * @param id
     * @return
     */
    @GetMapping("deleteAdvertisement")
    public ResultBean deleteAdvertisement(String id){
        return cjCarouselService.deleteAdvertisement(id);
    }

    /**
     * 分页查询零售商认证信息
     * @param model
     * @param antdPage
     * @return
     */
    @GetMapping(value= "queryRetailerByPage")
    public ResultBean queryRetailerByPage(CjRetailer model, AntdPage antdPage){
        return cjRetailerService.queryRetailerByPage(model, antdPage);
    }

    /**
     * 零售商认证审批
     * 并且更新客户表的省市县乡村数据
     * @param type  '1'：通过    '2'：拒绝
     * @return
     */
    @GetMapping("verificationRetailer")
    public ResultBean verificationRetailer(String id,String type){
        return cjRetailerService.verificationRetailer(id,type);
    }

    /**
     * 分页查询县级代理认证信息
     * @param model
     * @param antdPage
     * @return
     */
    @GetMapping(value= "queryCountyProxyByPage")
    public ResultBean queryCountyProxyByPage(CjCountyProxy model, AntdPage antdPage){
        return cjCountyProxyService.queryCountyProxyByPage(model, antdPage);
    }

    /**
     * 县级代理认证审批
     * @param type  '1'：通过    '2'：拒绝
     * @return
     */
    @GetMapping("verificationCountyProxy")
    public ResultBean verificationCountyProxy(String id,String type){
        return cjCountyProxyService.verificationCountyProxy(id,type);
    }

    /**
     * 分页查询机手认证
     * @param model
     * @param antdPage
     * @return
     */
    @GetMapping("queryRobotAttestByPage")
    public ResultBean queryRobotAttestByPage(CjRobotAttest model, AntdPage antdPage){
        return cjRobotAttestService. queryRobotAttestByPage(model,antdPage);
    }

    /**
     * 机手认证审核
     * @param id    主键id
     * @param type  1：通过    2：拒绝
     * @return
     */
    @GetMapping("verificationRobot")
    public ResultBean verificationRobot(String id,String type){
        return cjRobotAttestService.verificationRobot(id, type);
    }

    /**
     * 分页查询业务员与激活码关联
     * @param antdPage
     * @param model
     * @return
     */
    @GetMapping("queryEmployeeCodeByPage")
    public ResultBean queryEmployeeCodeByPage(AntdPage antdPage, CjActivationSale model){
        return cjActivationSaleService.queryByPage(antdPage, model);
    }

    /**
     * 新增业务员与激活码关联
     * @param model
     * @return
     */
    @PostMapping("addEmployeeCode")
    public ResultBean addEmployeeCode(@RequestBody CjActivationSale model){
        return cjActivationSaleService.addEmployeeCode(model);
    }

    /**
     * 删除业务员与激活码关联
     * @param id
     * @return
     */
    @GetMapping("deleteEmployeeCode")
    public ResultBean deleteEmployeeCode(String id){
        return cjActivationSaleService.deleteEmployeeCode(id);
    }

    /**
     * 查询所有的业务员
     * @return
     */
    @GetMapping("findEmployeeCode")
    public ResultBean findEmployeeCode(){
        return cjActivationSaleService.findEmployeeCode();
    }

    /**
     * 分页查询种地大户
     * @param model
     * @param antdPage
     * @return
     */
    @GetMapping("queryFarmByPage")
    public ResultBean queryFarmByPage(CjFarm model, AntdPage antdPage){
        return cjFarmService.queryFarmByPage(model, antdPage);
    }

    /**
     * 种地大户页面审核
     * @param id    主键id
     * @param type  1：通过    2：拒绝
     * @return
     */
    @GetMapping("verificationFarm")
    public ResultBean verificationFarm(String id,String type){
        return cjFarmService.verificationFarm(id,type);
    }

    /**
     * 分页查询满减规则
     * @param antdPage
     * @param model
     * @return
     */
    @GetMapping("queryRuleByPage")
    public ResultBean queryRuleByPage(AntdPage antdPage, CjRule model){
        return cjRuleService.queryRuleByPage(antdPage, model);
    }

    /**
     * 新增满减规则
     * @param model
     * @return
     */
    @PostMapping("addRule")
    public ResultBean addRule(@RequestBody CjRule model){
        return cjRuleService.addRule(model);
    }

    /**
     * 删除满减规则
     * @param id
     * @return
     */
    @GetMapping("deleteRule")
    public ResultBean deleteRule(String id){
        return cjRuleService.deleteRule(id);
    }

    /**
     * 修改满减规则
     * @param id    主键
     * @param money 满额度
     * @param saleMoney 减少额度
     * @return
     */
    @GetMapping("modifyRule")
    public ResultBean modifyRule(String id,String money,String saleMoney){
        return cjRuleService.modifyRule(id, money, saleMoney);
    }

    /**
     * 分页查询订单表
     * @param model
     * @param antdPage
     * @return
     */
    @GetMapping("queryOrderIndexWebByPage")
    public ResultBean queryOrderIndexWebByPage(CjOrder model, AntdPage antdPage){
        return cjOrderService.queryOrderIndexWebByPage(model, antdPage);
    }

    /**
     * 由于各种未知原因造成客户实际已经支付，但是系统未确认的
     * @param orderIndexId  一级订单号id
     * @return
     */
    @GetMapping("orderIndexConfirm")
    public ResultBean orderIndexConfirm(String orderIndexId){
        return cjOrderService.orderIndexConfirm(orderIndexId);
    }

    /**
     * web端订单分账
     * @param id    订单id
     * @return
     */
//    @GetMapping("placeOnOrder")
//    public ResultBean placeOnOrder(String id){
//        return cjOrderIndexService.placeOnOrder(id);
//    }

    /**
     * 退款确认
     * @param id    二级订单id
     * @return
     */
    @GetMapping("confirmRefund")
    public ResultBean confirmRefund(String id){
        return cjOrderService.confirmRefund(id);
    }
    /**
     * 不退款操作
     * @param id    二级订单id
     * @return
     */
    @GetMapping("confirmRefundRefuse")
    public ResultBean confirmRefundRefuse(String id){
        return cjOrderService.confirmRefundRefuse(id);
    }

    /**
     * 用于web页面展示平台和厂家的金额详情
     * @return
     */
    @GetMapping("pingTai")
    public ResultBean pingTai(){
        return cjOrderIndexService.pingTai();
    }






    /**
     * 此方法用于页面清除数据时使用，不能删除。
     *
     * @return
     */
    @GetMapping(value = "clearPurposeData")
    public ResultBean clearPurposeData() {
        return new ResultBean(ResultBean.SUCCESS);
    }

    /**
     * 测试初始化生成省份数据
     *
     */
    @GetMapping(value = "initArea")
    public void initArea() {
        fjCityService.initArea();

    }
}
